package com.ct.system.start.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface QueryMapper extends BaseMapper {

    @Select("SELECT\n" +
            "\tpk_BILLTEMPLET,\n" +
            "\tbill_templetcaption,\n" +
            "\tbase.name AS name,\n" +
            "\tmetadataclass \n" +
            "FROM\n" +
            "\tPUB_BILLTEMPLET temp\n" +
            "\tRIGHT JOIN (\n" +
            "\t\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\ttemplateid,\n" +
            "\t\t\t( SELECT max( fun_name ) FROM sm_funcregister WHERE upper( funcode ) = upper( funnode ) ) name \n" +
            "\t\tFROM\n" +
            "\t\t\tpub_systemplate_base \n" +
            "\t\tWHERE\n" +
            "\t\t\tupper( funnode ) IN ( SELECT upper( funcode ) FROM sm_funcregister ${ew.customSqlSegment} ) \n" +
            "\t\t) \n" +
            "\t) base ON temp.PK_BILLTEMPLET = base.templateid \n" +
            "WHERE\n" +
            "\tmetadataclass IS NOT NULL")
    List<Map<String, Object>> queryTemplate(@Param(Constants.WRAPPER) QueryWrapper<Map<String,Object>> qw);

    @Select("select displayname,name,namespace,ownmodule,id,versiontype from md_component ${ew.customSqlSegment}")
    List<Map<String, Object>> queryComponent(@Param(Constants.WRAPPER) QueryWrapper<Map<String,Object>> qw);

    @Select("SELECT\n" +
            "\tdisplayname,name,namespace,ownmodule,id,versiontype \n" +
            "FROM\n" +
            "\tMD_COMPONENT \n" +
            "WHERE\n" +
            "\tid IN ( SELECT COMPONENTID FROM md_class WHERE LOWER( defaulttablename ) IN ( SELECT LOWER( table_name ) FROM user_tables ${ew.customSqlSegment} ) )")
    List<Map<String, Object>> queryComponent2(@Param(Constants.WRAPPER) QueryWrapper<Map<String,Object>> qw);

    @Select("SELECT\n" +
            "\tdisplayname,name,namespace,ownmodule,id,versiontype \n" +
            "FROM\n" +
            "\tMD_COMPONENT \n" +
            "WHERE\n" +
            "\tid IN ( SELECT COMPONENTID FROM md_class  ${ew.customSqlSegment} )")
    List<Map<String, Object>> queryComponent3(@Param(Constants.WRAPPER) QueryWrapper<Map<String,Object>> qw);

    @Select("select (displayname ||' / '|| fullclassname ||' / '|| LOWER(defaulttablename)) as title,id,defaulttablename from md_class ${ew.customSqlSegment}")
    List<Map<String,Object>> queryClasses(@Param(Constants.WRAPPER) QueryWrapper<Map<String,Object>> qw);

    @Select("select (select DISPlayNAME from md_class where id = p.datatype) cname,(select id from md_class where p.datatype = id) cid,(select max(id) from MD_ENUMVALUE where id=p.datatype) eid,DISPlayNAME,datatype,name, attrlength,nullable from MD_PROPERTY p ${ew.customSqlSegment}")
    List<Map<String,Object>> queryPropertys(@Param(Constants.WRAPPER) QueryWrapper<Map<String,Object>> qw);

    @Select("select NAME,VALUE from MD_ENUMVALUE ${ew.customSqlSegment}")
    List<Map<String,Object>> queryEnums(@Param(Constants.WRAPPER) QueryWrapper<Map<String,Object>> qw);

    @Select("select displayname,fullclassname,defaulttablename from md_class ${ew.customSqlSegment}")
    Map<String, Object> queryClass(@Param(Constants.WRAPPER) QueryWrapper<Map<String,Object>> qw);

    @Select("select (displayname ||' / '|| fullclassname ||' / '|| defaulttablename) as title,id,defaulttablename from md_class ${ew.customSqlSegment}")
    List<Map<String,Object>> queryClassesByTabName(@Param(Constants.WRAPPER) QueryWrapper<Map<String,Object>> qw);

    @Select("select LOWER(table_name) from user_tables ${ew.customSqlSegment}")
    List<String> queryDBTableName(@Param(Constants.WRAPPER) QueryWrapper<String> qw);

    List<Map<String,String>> queryTableData(@Param("tableName")String tableName,@Param("startNum")int startNum,@Param("endNum")int endNum);

    int queryTableDataCount(@Param("tableName")String tableName);

}
